        // global variabless
        var videos;     // holds the video list from the youtube api request returned data
        var player;     // the youtube player object
        
        // build the api request url
        url = "https://gdata.youtube.com/feeds/api/";   // base url
        // if an author is supplied, get that authors videos
        if(author.length > 0) {
            url = url + "users/" + author + "/uploads?alt=json&v=2";    // url for author videos and output settings
            url = url + "&orderby="+orderby;                            // how to order the videos
        }
        // if a playlist is supplied, get the playlist videos
        else if(playlist.length > 0) {
            url = url + "playlists/" + playlist + "?alt=json&v=2";      // url for playlist videos and output settings
            // playlists have their own order
        }
        // if a search query is supplied, get the result videos
        else if(search.length > 0) {
            url = url + "videos?alt=json&v=2&q=" + search;              // url for searched result videos and output settings
            url = url + "&orderby="+orderby;                            // how to order the videos
        }
        // if nothing is supplied show an error
        else {
            showError('Error: no feed details supplied.');
        }
        // if maxResults is 0, omit this parameter to retrieve all the videos
        if(maxResults > 0) {
            url = url + "&max-results="+maxResults; // the number of videos to include
        }
        url = url + "&start-index=1";               // the index at which to start retrieving the videos
        
        // make a request to the youtube api and store in a variable for future/global reference.
        var jqxhr = $.getJSON(url);
        
        // this function is called once the iframe is loaded with the player ready
        function onPlayerReady(event) {
          // show the main div
          $('#layout-container').show();
          // load first video
          loadVideo(playVideo);
        }
        
        // this function is used to load a video from the global videos object array
        function loadVideo(videoIndex) {
          // check video exists
          if(videos[videoIndex] === undefined) {
            showError('Error: unknown default video index.');
          }
          
          // store video in variable for easy reference
          var video = videos[videoIndex];
          
          // create a new date object using the selected videos info
          var d = new Date(video.published.$t);
         
          // update page for selected video
          $('#vid-title').html(video.title.$t);
          $('#vid-views').html((video.yt$statistics === undefined ? 0 : video.yt$statistics.viewCount) + ' views');
          $('#vid-realdate').html(d.getDate() + '/' + (d.getMonth() + 1) + '/' + d.getFullYear());
          $('#vid-desc').html(video.media$group.media$description.$t);
          
          // play the selected video
          //player.loadVideoById(video.id.$t.split(/[: ]+/).pop(), 0, quality);
          player.loadVideoById(video.media$group.yt$videoid.$t, 0, quality);
        }
        
        // this function handles errors
        function showError(text) {
          // hide the main div
          $('#layout-container').hide();
          // show error container
          $('#error-container').show();
          // insert supplied error text
          $('#error-container p').html(text);
          
        }
        
        // after the document has loaded
        $(document).ready(function() {
            
          // on failure of the youtube api request
          jqxhr.error(function(jqXHR, textStatus, errorThrown) {
            // use custom error function to show error
            showError('Error: Failed to process the API request.');
          });
          
          // on sucess of the youtube api request
          jqxhr.success(function(gdata) {
            // check if there are any videos returned
            if (gdata.feed.entry === undefined) {
              showError('Error: No videos found for this author.');
            } else {
              // store the video list from the youtube api request returned data into the reserved/global variable
              videos = gdata.feed.entry;
            
              // load the player and store the player object in the reserved/global variable
              player = new YT.Player('player', {
                height: '318',
                width: '565',
                //playerVars: { 'autoplay': autoPlay },
                //videoId: 'u1zgFlCw8Aw',
                events: {
                  'onReady': onPlayerReady
                }
              });
              
              // iterate through each of the videos in the video list
              $.each(videos, function(index) {
                // setup the default thumbnail image
                thumbnail = 'http://s.ytimg.com/yts/img/no_thumbnail-vfl4t3-4R.jpg';
                // if a thumbnail exists, use it.
                if(videos[index].media$group.media$thumbnail !== undefined) {
                   thumbnail = videos[index].media$group.media$thumbnail[0].url 
                }
                
                // append each video to the html list
                $('ul#videolist').append(
                  $('<li>').append(
                    $('<img>')
                      .attr('src', thumbnail)
                      .attr('title', videos[index].title.$t)
                      .attr('id', 'video-'+index)
                  )
                );
              });
            }
          });
          
          
          // catch the click event on the previous videos thumbnails
          $('ul#videolist').on('click', 'img', function() {
            // load the video using the video index from the id attribute (video-x) where x is the video index
            loadVideo($(this).attr('id').split('-').pop());
          })
          
          // update the link to go to the authors youtube page
          $('#videolist-more')
            .attr('href','http://www.youtube.com/user/'+author)
              .attr('title','View More Videos');
         
        });